package qianting.news.article.controller;

import cn.hutool.core.lang.Assert;
import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qianting.news.apis.models.article.pojos.ApArticle;
import qianting.news.apis.models.article.pojos.ApArticleContent;
import qianting.news.article.service.ApArticleConfigService;
import qianting.news.article.service.ApArticleContentService;
import qianting.news.article.service.ApArticleService;
import qianting.news.basic.filecenter.service.FileStorageService;
import qianting.news.common.constant.FilePrefixConstants;
import qianting.news.common.entity.State;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "文章内容管理")
@RestController
@RequestMapping("/")
public class ArticleContentController {

    @Autowired
    private ApArticleService apArticleService;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @ApiOperation(value = "获取文章内容")
    @GetMapping("getArticleContent/{id}")
    public State getArticleContent(@PathVariable("id") Long id){

        ApArticle article = apArticleService.getById(id);
        String staticUrl = article.getStaticUrl();
        Assert.notNull(staticUrl);
        return State.succ(staticUrl);

    }

    //将文章的内容静态化为html文件上传到minio中去(模板)
    @ApiOperation(value = "将文章的内容静态化为html文件上传到minio中去(模板)")
    @GetMapping("createStaticHtml/{id}")
    public State createStaticHtml(@PathVariable("id") Long id) throws Exception{

        //获取文章的内容
        List<ApArticleContent> list = apArticleContentService.list();

        for (ApArticleContent articleContent:list){
            if(articleContent==null|| StringUtils.isBlank(articleContent.getContent())) return State.fail("文章不存在");

            //通过freemarker生成html文件
            StringWriter out = new StringWriter();
            Template template = configuration.getTemplate("article.ftl");

            Map<String, Object> params = new HashMap<>();
            params.put("content", JSONArray.parseArray(articleContent.getContent()));

            template.process(params, out);
            InputStream is = new ByteArrayInputStream(out.toString().getBytes());

            //3.把html文件上传到minio中
            String path = fileStorageService.uploadHtmlFile(FilePrefixConstants.ARTICLE_CONTENT_PREFIX, articleContent.getArticleId() + ".html", is);

            //4.修改ap_article表，保存static_url字段
            Long articleId = articleContent.getArticleId();
            apArticleService.updateStaticUrlById(articleId,path);
        }
        return State.succ();

    }

}
